import { defineConfig } from "vitepress";
import path from "node:path";
import fs from "node:fs";
import electronConfig from "./electron";
import nodeJsConfig from "./nodejs";
import mysqlConfig from "./mysql";
import basicConfig from "./basic";
import three from "./three.json";
import nodeJs from "./node.js.json";
import markdownIt from "./markdown-it.json";
import animd from "./animd.json";

const [vuePath, vueItem] = useFunction("/vue/");
const [vitePath, viteItem] = useFunction("/vite/");
const [nodeJsPath, nodeJsItem] = useFunction("/node.js/");
const [elementPlusPath, elementPlusItem] = useFunction("/element-plus/");
const [axiosPath, axiosItem] = useFunction("/axios/");
const [bootstrapPath, bootstrapItem] = useFunction("/bootstrap/");
const [mysqlLink] = useFunction("/packages/mysql/");
const [reactPath, reactItem] = useFunction("/react/");

export default defineConfig({
    title: "Js Study World",
    description: "Js Study World",
    base: "/study-js/",
    themeConfig: {
        nav: [
            { text: "前端基础", link: "/basic/" },
            { text: "Node.js", link: nodeJsPath() },
            { text: "Vite", link: vitePath() },
            { text: "Vue", link: vuePath() },
            { text: "React", link: reactPath() },
            { text: "Axios", link: axiosPath() },
            { text: "Electron", link: "/electron/" },
            { text: "Three.js", link: "/three/" },
            { text: "更新日志", link: "/log" },
            {
                text: "UI-Framework",
                items: [
                    { text: "Bootstrap", link: bootstrapPath() },
                    { text: "Element Plus", link: elementPlusPath() },
                ],
            },
            {
                text: "Packages",
                items: [
                    { text: "imshann/mysql", link: mysqlLink() },
                    { text: "Markdown It", link: "/markdown-it/" },
                    { text: "Animd", link: "/animd/" },
                ],
            },
        ],

        sidebar: {
            "/": basicConfig,
            "/animd/": [
                {
                    text: "基础",
                    items: animd,
                },
            ],
            "/markdown-it/": [
                {
                    text: "基础",
                    items: markdownIt,
                },
            ],
            "/three/": [
                {
                    text: "基础",
                    items: three,
                },
            ],
            [mysqlLink()]: mysqlConfig,
            "/electron/": electronConfig,
            [bootstrapPath()]: [
                {
                    text: "Bootstrap 基础",
                    items: [bootstrapItem()],
                },
                {
                    text: "Bootstrap 进阶",
                    items: [],
                },
            ],
            [elementPlusPath()]: [
                {
                    text: "Element Plus 基础",
                    items: [elementPlusItem()],
                },
                {
                    text: "Element Plus 进阶",
                    items: [],
                },
            ],
            [nodeJsPath()]: [
                {
                    text: "Node.js 基础",
                    items: nodeJs,
                },
            ],
            [vuePath()]: [
                {
                    text: "Vue 基础",
                    items: [
                        vueItem(),
                        vueItem("basic/install"),
                        vueItem("basic/component"),
                        // vueItem("component"),
                        vueItem("template"),
                        vueItem("script"),
                        vueItem("api"),
                    ],
                },
                {
                    text: "Vue 进阶",
                    items: [],
                },
            ],
            [vitePath()]: [
                {
                    text: "Vite 基础",
                    items: [viteItem(), viteItem("install")],
                },
                {
                    text: "Vite 进阶",
                    items: [],
                },
            ],
            [reactPath()]: [
                {
                    text: "React 基础",
                    items: [reactItem(), reactItem("hook")],
                },
                {
                    text: "React 进阶",
                    items: [],
                },
            ],
            // "/basic/": basicConfig,
        },

        socialLinks: [
            { icon: "github", link: "https://gitee.com/imshann/study-js.git" },
        ],
    },
    markdown: {
        config(md) {
            md.use(function (md) {
                const fence = md.renderer.rules.fence!;
                md.renderer.rules.fence = (...args) => {
                    const [tokens, idx] = args;
                    const token = tokens[idx];
                    const result = fence(...args);
                    if (!token.content) {
                        return result;
                    }
                    token.content = token.content.replace(
                        /.*#(end)*region.*\r?\n/g,
                        ""
                    );
                    return result;
                };
            });
        },
    },
});

function createFunction(rootPath: string) {
    return (path: string = "") => {
        return rootPath + path;
    };
}

function createWithTextFunction(rootPath: string) {
    return (filePath: string = "") => {
        const relativePath = "./" + rootPath + filePath;
        let absolutePath = path.resolve(relativePath);
        let text = "";
        if (filePath === "") {
            absolutePath += "/index.md";
        } else {
            absolutePath += ".md";
        }
        if (fs.existsSync(absolutePath)) {
            const content = fs.readFileSync(absolutePath, {
                encoding: "utf-8",
            });
            if (content.length > 0) {
                const result = content.match(/#\ (.*?)\r\n/);
                if (result !== null) {
                    text = result[1];
                }
            }
        }
        return {
            text: text,
            link: rootPath + filePath,
        };
    };
}

function useFunction(filePath: string): [
    (path?: string) => string,
    (filePath?: string) => {
        text: string;
        link: string;
    }
] {
    const func = createFunction(filePath);
    const funcWithText = createWithTextFunction(filePath);
    return [func, funcWithText];
}
